package com.hd.hdxgddcommon.aop.entity;

import com.hd.hdxgddcommon.utils.ReflectBase;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/11/5.
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class LogAnParse extends ReflectBase {

    private static final String HEAD = "#";

    private String[] content;
    private Object value;
    private String completeContent;


    public void setCompleteContent(String val) {
        val = val.substring(2, val.length() - 1);
        String[] split = val.split("\\.");
        setContent(split);
        this.completeContent = val;
    }

    public String getParseName(boolean addHead) {

        if (content != null && content.length != 0) {
            String parseName = content[0];
            if (addHead) {
                parseName = HEAD + parseName;
            }
            return parseName;
        }
        return null;
    }

    public String getFinalName(boolean addHull, String head) {
        String content = completeContent;
        if (addHull) {
            StringBuffer sb = new StringBuffer();
            sb.append(head).append("{").append(content).append("}");
            content = sb.toString();
        }
        return content;
    }

    public String getFinalValue() {
        try {
            if (content != null) {
                int length = content.length;
                if (length != 1) {
                    for (int i = 1; i < length; i++) {
                        if (value != null) {
                            value = getNextObject(value, content[i]);
                        }
                    }
                }
                return value == null ? null : value.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    private Object getNextObject(Object value, String filedName) throws Exception {
        Object t = value;
        Class<?> aClass = value.getClass();
        Method method = aClass.getMethod(fieldNameToMethodName(GET, filedName));
        Object invoke = method.invoke(t);
        return invoke;
    }

}
